//
//  ZXSegmentedView.m
//  ZXSegmentedView
//
//  Created by LIUKUN-PC on 16/5/4.
//  Copyright © 2016年 liukun. All rights reserved.
//

#import "ZXSegmentedView.h"
#import "ZXCategories.h"

#import "ZXContentAutoFit.h"

@interface ZXSegmentedView ()

/** 未选中的按钮背景颜色,  默认为 0xE4E4E4(浅灰) */
@property (nonatomic, strong) UIColor *unselectedBackgroundColor;
/** 选中的按钮背景颜色,  默认为 白色 */
@property (nonatomic, strong) UIColor *selectedBackgroundColor;
/** 未选中的标题颜色,  默认为  黑色 */
@property (nonatomic, strong) UIColor *unselectedTitleColor;
/** 选中的标题颜色,  默认为 0x3399FF(浅蓝) */
@property (nonatomic, strong) UIColor *selectedTitleColor;

@property (nonatomic, assign) NSInteger currentIndex;


@property (nonatomic, strong) NSArray<UIButton *> *buttonsArray;

@property (nonatomic, copy) void (^didSelectHandler)(NSInteger index);

@property (nonatomic, strong) UIView *lineView;

@end

@implementation ZXSegmentedView

#pragma mark - Public API

- (void)didSelectHandler:(void (^)(NSInteger))handlerBlock {
    self.didSelectHandler = handlerBlock;
}

- (void)selectAtIndex:(NSInteger)index {
    
    if (index < 0) {
        self.currentIndex = 0;
    } else if (index >= self.titlesArray.count) {
        self.currentIndex = self.titlesArray.count - 1;
    } else {
        self.currentIndex = index;
    }
}

- (void)setUnselectedBackgroundColor:(UIColor *)unselectedBackgroundColor selectedBackgroundColor:(UIColor *)selectedBackgroundColor unselectedTitleColor:(UIColor *)unselectedTitleColor selectedTitleColor:(UIColor *)selectedTitleColor {
    if (unselectedBackgroundColor) {
        self.unselectedBackgroundColor = unselectedBackgroundColor;
    }
    if (selectedBackgroundColor) {
        self.selectedBackgroundColor = selectedBackgroundColor;
    }
    if (unselectedTitleColor) {
        self.unselectedTitleColor = unselectedTitleColor;
    }
    if (selectedTitleColor) {
        self.selectedTitleColor = selectedTitleColor;
    }
    [self reloadUI];
}

- (instancetype)initWithframe:(CGRect)frame titles:(NSArray<NSString *> *)titlesArray {
    if (self = [super initWithFrame:frame]) {
        self.titlesArray = titlesArray;
    }
    return self;
}

#pragma mark - Getter & Setter

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        [self addSubview:_lineView];
    }
    return _lineView;
}

- (void)setHiddenLineView:(BOOL)hiddenLineView {
    _hiddenLineView = hiddenLineView;
    [self reloadUI];
}

- (void)setFontSize:(NSInteger)fontSize {
    _fontSize = fontSize;
    if (!fontSize || !_buttonsArray) {
        return;
    }
    for (UIButton *button in self.buttonsArray) {
        button.titleLabel.font = [UIFont systemFontOfSize:_fontSize];
    }
}

- (void)setTitlesArray:(NSArray<NSString *> *)titlesArray {
    _titlesArray = titlesArray;
    self.unselectedBackgroundColor = [UIColor colorWithHex:0xE4E4E4];
    self.selectedBackgroundColor = [UIColor whiteColor];
    self.unselectedTitleColor =COLOR_DIV ;
    self.selectedTitleColor = [UIColor colorWithHex:0x29b566];
    
    [self reloadUI];
}

- (void)setCurrentIndex:(NSInteger)currentIndex {
    if (_currentIndex != currentIndex) {
        _currentIndex = currentIndex;
        
        [self reloadUI];
        
        if (self.didSelectHandler) {
            self.didSelectHandler(_currentIndex);
        }
        [self.delegate segmentedView:self didSelectButtonAtIndex:_currentIndex];
    }
}

- (NSArray<UIButton *> *)buttonsArray {
    if (_buttonsArray) {
        return _buttonsArray;
    }
    [self.superview setNeedsLayout];
    [self.superview layoutIfNeeded];//适应AutoLayout
    
    NSMutableArray *mutableArray = [NSMutableArray array];
    for (int i = 0; i < self.titlesArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(i * self.frame.size.width / self.titlesArray.count, 0, self.frame.size.width / self.titlesArray.count, self.frame.size.height);
        button.tag = i;
        button.titleLabel.font = [UIFont systemFontOfSize:self.fontSize ? self.fontSize : 15];
        [button setTitle:self.titlesArray[i] forState:UIControlStateNormal];
        
        [button addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [mutableArray addObject:button];
        [self addSubview:button];
    }
    _buttonsArray = [mutableArray copy];
    
    return _buttonsArray;
}

#pragma mark - Action

- (void)touchButton: (UIButton *)sender {
    self.currentIndex = sender.tag;

}

- (void)reloadUI {
    
    for (UIButton *button in self.buttonsArray) {
        if (button.tag == self.currentIndex) {
            button.backgroundColor = self.selectedBackgroundColor;
            [button setTitleColor:self.selectedTitleColor forState:UIControlStateNormal];
        } else {
            button.backgroundColor = self.hiddenLineView ? self.unselectedBackgroundColor : self.selectedBackgroundColor;
            [button setTitleColor:self.unselectedTitleColor forState:UIControlStateNormal];
        }
    }
    
    if (!self.hiddenLineView) {
        self.lineView.backgroundColor = self.selectedTitleColor;
        
        UIButton *button = self.buttonsArray[self.currentIndex];
        
        CGFloat width = MIN(CGRectGetWidth(button.frame), [button.titleLabel.text widthWithMaxHeight:21 fontSize:self.fontSize ? : 15] + 30);
        [UIView animateWithDuration:0.2 animations:^{
            self.lineView.frame = CGRectMake(button.center.x - width / 2, CGRectGetMaxY(button.frame) - 1, width, 1);
        }];

    }
    
    
    
}


@end
